package com.cwt_taxi_xa.db.dal;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import com.cwt_taxi_xa.util.AppLog;

/**
 * sqlite 数据库升级器
 * @author wlh
 */
public class DatabaseUpdater {

	public static final String TAG = "DatabaseUpdater";
	private static ContentValues values = new ContentValues();

	public static void update(SQLiteDatabase db, int oldDBVersion){
		switch (oldDBVersion) {
			case 0:		// 升级数据库0
				try {
					AppLog.i(TAG, "更新数据库0");
					addDriver(db);
					addInvitation(db);
					addSuccessBidder(db);
					addContacts(db);
					addEvent(db);
					addQuestion(db);
					addMessage(db);
					addLocalTable(db);
					addPhotoInfo(db);
					addRecordingInfo(db);
					addFence(db);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					AppLog.e(TAG, "更新数据库0出错");
				}
				break;
			default:
				break;
		}
	}

	private static void addRecordingInfo(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库添加录音文件数据表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_RECORDING+"("+
					DBConstants.REC_ID+" integer primary key not null,"+
					DBConstants.REC_REASON+" integer not null,"+
					DBConstants.REC_START_LAT +" integer not null,"+
					DBConstants.REC_START_LNG +" integer not null,"+
					DBConstants.REC_END_LAT +" integer,"+
					DBConstants.REC_END_LNG +" integer,"+
					DBConstants.REC_END_TIME+" text,"+
					DBConstants.REC_MEDIA_ID+" text not null,"+
					DBConstants.REC_TIME+" text not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.i(TAG, "数据库添加录音文件数据表出错！");
		}
	}

	/**
	 * 添加司机信息表
	 * @param db
	 */
	private static void addDriver(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "司机信息表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_DRIVER+"("+
					DBConstants.DRIVER_ID+" integer primary key not null,"+
					DBConstants.DRIVER_NAME+" text not null,"+
					DBConstants.DRIVER_COMPANY+" text not null,"+
					DBConstants.DRIVER_COMPANY_NAME+" text not null,"+
					DBConstants.DRIVER_CARD_CODE+" text not null,"+
					DBConstants.DRIVER_CERTIFICATE_CODE+" text not null,"+
					DBConstants.DRIVER_VALIDITY_DATE+" text not null,"+
					DBConstants.DRIVER_CAR_NUMBER+" text not null,"+
					DBConstants.DRIVER_NEXT_MEETING_TIME+" text not null,"+
					DBConstants.DRIVER_SERVICE_TEL+" text not null,"+
					DBConstants.DRIVER_IC_CLASS+" integer not null,"+
					DBConstants.DRIVER_TAXI_STAR+" integer not null,"+
					DBConstants.DRIVER_BLACK_INFO+" integer not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加司机信息表出错！");
		}
	}

	/**
	 * 添加照片数据表
	 * @param db
	 */
	private static void addPhotoInfo(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库照片数据表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_PHOTO+"("+
					DBConstants.PHOTO_ID+" integer primary key not null,"+
					DBConstants.PHOTO_MEDIA_ID+" text not null, "+
					DBConstants.PHOTO_TIME+" text not null, "+
					DBConstants.PHOTO_SERIAL_ID+" text, "+
					DBConstants.PHOTO_EVENT_CODE+" integer not null, "+
					DBConstants.PHOTO_CHANNEL_ID+" integer not null, "+
					DBConstants.PHOTO_RESOLUTION+" integer not null, "+
					DBConstants.PHOTO_LAT+" integer not null, "+
					DBConstants.PHOTO_LNG+" integer not null, "+
					DBConstants.PHOTO_LOCATION_INFO+" text not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加照片数据表出错！");
		}
	}

	/**
	 * 添加本地数据表
	 * @param db
	 */
	private static void addLocalTable(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库添加本地数据表");
		try {
			/*db.execSQL("create table business(id integer primary key autoincrement not null," +
					"message text)");*/
			db.execSQL("create table "+DBConstants.TABLE_LOCAL+"("+
					DBConstants.LOCAL_ID+" integer primary key not null,"+
					DBConstants.LOCAL_TYPE+" integer not null,"+
					DBConstants.LOCAL_STATUS+" integer not null,"+
					DBConstants.LOCAL_TIME+" text,"+
					DBConstants.LOCAL_DATE+" text,"+
					DBConstants.LOCAL_CAR_TIMES+" text,"+
					DBConstants.LOCAL_METER_CODE+" text,"+
					DBConstants.DRIVER_CERTIFICATE_CODE+" text,"+
					DBConstants.LOCAL_DATA+" text not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加本地数据表出错！");
		}
	}

	private static void addEvent(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库添加事件表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_EVENT+"("+
					DBConstants.EVENT_ID+" integer not null,"+
					DBConstants.EVENT_TEXT+" text, "+
					DBConstants.EVENT_CREATE_TIME+" text not null, "+
					DBConstants.EVENT_IS_DONE+" integer, "+
					DBConstants.EVENT_IS_READ+" integer"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加事件表出错！");
		}
	}

	private static void addQuestion(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库添加提问信息表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_QUESTION+"("+
					DBConstants.QUESTION_ID+" integer not null,"+
					DBConstants.QUESTION_TEXT+" text not null, "+
					DBConstants.QUESTION_ANSWER+" text not null, "+
					DBConstants.QUESTION_CREATE_TIME+" text not null, "+
					DBConstants.QUESTION_FLAG+" text not null, "+
					DBConstants.QUESTION_SERIAL_ID+" integer not null, "+
					DBConstants.QUESTION_IS_READ+" integer not null, "+
					DBConstants.QUESTION_ANSWER_ID+" integer, "+
					DBConstants.QUESTION_IS_DONE+" integer not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加提问信息表出错！");
		}
	}

	private static void addContacts(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库联系人表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_CONTACTS+"("+
					DBConstants.CONTACTS_ID+" integer primary key autoincrement not null,"+
					DBConstants.CONTACTS_TYPE+" integer not null, "+
					DBConstants.CONTACTS_NAME+ " text not null, "+
					DBConstants.CONTACTS_PHONE+" text not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加联系人表出错！");
		}
	}

	private static void addFence(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		AppLog.i(TAG, "数据库添加围栏表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_FENCE+"("+
					DBConstants.FENCE_ID+" integer primary key autoincrement not null,"+
					DBConstants.FENCE_TYPE+" integer not null, "+
					DBConstants.FENCE_AREA_ID+ " integer not null, "+
					DBConstants.FENCE_AREA_ATTRB+" text not null, "+
					DBConstants.FENCE_START_TIME+" text, "+
					DBConstants.FENCE_END_TIME+" text, "+
					DBConstants.FENCE_MAX_SPEED+" integer, "+
					DBConstants.FENCE_TIME_LENGTH+" integer, "+
					DBConstants.FENCE_TOP_LIGHT_MSG+" text, "+
					DBConstants.FENCE_DRIVER_MESSAGE+" text, "+
					DBConstants.FENCE_CENTRE_LNG+" text, "+
					DBConstants.FENCE_CENTRE_LAT+" text, "+
					DBConstants.FENCE_RADIUS+" text, "+
					DBConstants.FENCE_LEFT_TOP_LAT+" text, "+
					DBConstants.FENCE_LEFT_TOP_LNG+" text, "+
					DBConstants.FENCE_RIGHT_BOTTOM_LAT+" text, "+
					DBConstants.FENCE_RIGHT_BOTTOM_LNG+" text"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			AppLog.e(TAG, "数据库添加围栏表出错！");
		}
	}

	/**
	 * 添加文本信息表
	 * @param db
	 */
	public static void addMessage(SQLiteDatabase db){

		AppLog.i(TAG, "数据库添加文本信息表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_MESSAGE+"("+
					DBConstants.MESSAGE_ID+" integer primary key not null,"+
					DBConstants.MESSAGE_CONTENT+" text not null, "+
					DBConstants.MESSAGE_CREATE_TIME+" text not null, "+
					DBConstants.MESSAGE_IS_READ+" integer not null"+
					//					DBConstants.MESSAGE_FLAG+" integer not null, "+
					//					DBConstants.MESSAGE_SERIAL+" integer not null, "+
					//					DBConstants.MESSAGE_TIME_LENGTH+" integer not null"+
					//					DBConstants.MESSAGE_TTS+" integer not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加文本信息表出错！");
		}
	}

	/**
	 * 添加招标信息表
	 * @param db
	 */
	public static void addInvitation(SQLiteDatabase db){

		AppLog.i(TAG, "数据库添加招标信息表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_INVITATION+"("+
					DBConstants.INVITATION_ID+" integer primary key not null,"+
					DBConstants.INVITATION_ORDER_ID+" text not null, "+
					DBConstants.INVITATION_MESSAGE+" text, "+
					DBConstants.INVITATION_CREATE_TIME+" text not null, "+
					DBConstants.DRIVER_CERTIFICATE_CODE+" text, "+
					DBConstants.INVITATION_ORDER_TYPE+" integer not null, "+
					DBConstants.INVITATION_ORDER_TIME+" text, "+
					DBConstants.INVITATION_REMARK+" text, "+
					DBConstants.INVITATION_STATUS+" integer not null"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加招标信息表出错！");
		}
	}

	/**
	 * 添加中标信息表
	 * @param db
	 */
	public static void addSuccessBidder(SQLiteDatabase db){

		AppLog.i(TAG, "数据库添中标信息表");
		try {
			db.execSQL("create table "+DBConstants.TABLE_SUCCESSFUL_BIDDER+"("+
					DBConstants.SB_ID+" integer primary key not null,"+
					DBConstants.SB_STATUS+" integer not null, "+
					DBConstants.SB_ORDER_ID+" text not null, "+
					DBConstants.SB_PASSENGER_PHONE+" text, "+
					DBConstants.SB_MESSAGE+" text, "+
					DBConstants.SB_DO_STATUS+" integer, "+
					DBConstants.SB_IS_DONE+" integer not null, "+
					DBConstants.SB_CREATE_TIME+" text not null, "+
					DBConstants.SB_IS_PUSH+" integer not null, "+
					DBConstants.SB_ORDER_TYPE+" integer not null, "+
					DBConstants.DRIVER_CERTIFICATE_CODE+" text, "+
					DBConstants.SB_ORDER_TIME+" text, "+
					DBConstants.SB_FEE+" text, "+
					DBConstants.ORDER_LNG_START+" text, "+
					DBConstants.ORDER_LNG_END+" text, "+
					DBConstants.ORDER_LAT_END+" text, "+
					DBConstants.ORDER_LAT_START+" text, "+
					DBConstants.SB_REMARK+" text"+
					")");
		} catch (Exception e) {
			// TODO: handle exception
			AppLog.e(TAG, "数据库添加中标信息表出错！");
		}
	}
}
